﻿using System;
using System.Web;
using System.ServiceModel;
using ActionService;
using ActionService.Messages;
using ActionService.MessageBase;
using ActionService.Criteria;
using ActionService.DataTransferObjects;

namespace ASPNETWebApplication.Repositories
{
    /// <summary>
    /// Base class for all Repositories. Manages Service client.
    /// Provides common request-response correlation check.
    /// </summary>
    public abstract class RepositoryBase
    {
        /// <summary>
        /// Lazy loads ActionServiceClient and stores it in Session object.
        /// </summary>
        protected ActionService.ServiceImplementations.ActionService Client
        {
            get
            {
                if (HttpContext.Current.Session["ActionServiceClient"] == null) 
                    HttpContext.Current.Session["ActionServiceClient"] = new ActionService.ServiceImplementations.ActionService(); 
                return HttpContext.Current.Session["ActionServiceClient"] as ActionService.ServiceImplementations.ActionService;
            }
        }
        protected void Correlate(RequestBase request, ResponseBase response)
        {
            if (request.RequestId != response.CorrelationId)
                throw new ApplicationException("RequestId and CorrelationId do not match.");
        }
    }
}
